package gameObjects;

import java.util.ArrayList;

import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.Touchable;

public class CardPile extends Actor {

	private ArrayList<Card> pileList = new ArrayList<Card>();
	private CardTable table;
	private float cardWidth = 575.0f / 8.0f;
	private float cardHeight = 450.0f / 4.0f;

	public CardPile(CardTable _table) {
		table = _table;
		this.setOrigin(0, 0);
		this.setPosition((480 / 2) + 10, (800 / 2) - cardHeight / 2);
		this.setTouchable(Touchable.enabled);
		this.setSize(cardWidth, cardHeight);
		this.addListener(new InputListener() {

			public boolean touchDown(InputEvent event, float x, float y,
					int pointer, int button) {
				System.out.println("pile down");
				System.out.println("pile suit "
						+ pileList.get(pileList.size() - 1).getSuit());
				System.out.println("pile value "
						+ pileList.get(pileList.size() - 1).getValue());
				

				return true;
			}

			@Override
			public void touchUp(InputEvent event, float x, float y,
					int pointer, int button) {
				if (table.getStackCount() == 0) {
					table.reShuffleRemainingCards(pileList);

					resetPile();

				}
				System.out.println("pile up");

			}

		});

	}

	public int getTopSuit() {

		return pileList.get(pileList.size() - 1).getSuit();

	}

	public int getTopValue() {

		return pileList.get(pileList.size() - 1).getValue();
	}

	public void addCard(Card _card) {
		if (pileList.size() > 0)
			pileList.get(pileList.size() - 1).remove();
		_card.setPosition(this.getX(), this.getY());
		pileList.add(_card);

	}

	private int pileCount() {
		return pileList.size();
	}

	private void resetPile() {
		pileList.get(pileList.size() - 1).remove();
		pileList.clear();
	}

	@Override
	public void act(float delta) {
		super.act(delta);
		if (pileList.size() != 0)
			pileList.get(pileList.size() - 1).act(delta);

	}
}
